var myWalletCtrl  = new XFApp.client();
$.views.converters({	
	fmoney : function(value) {		
		if(!value){
			return "0.00";
		}		
		return moneyCtrl.money.fmoney(value,2);		
	},
});

XFApp.client.prototype.myWallet = {
		successCode : 10000,	
		walletPage : null,
		urlItems : {
			initListURL: basePath+"/userCenter/wallet/query",
		},
		
		event : function (){			
			_this = this;		
			var reqTimeStart ;
			var reqTimeEnd ;
			var date = new Date(); 
			var transactionType = 0;			
			//默认查询当天
			var dateArr = date.dateChange( "thisday" );	
			
			//收支明细
			$("#all").on("click",function(){				
				$('#paymentType #recharge').removeClass('active');
				$('#paymentType #withdraw').removeClass('active');
				$('#paymentType #payment').removeClass('active');
				$('#paymentType #all').addClass('active');				
				transactionType = 0 ;				
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//充值记录
			$("#recharge").on("click",function(){								
				$('#paymentType #all').removeClass('active');
				$('#paymentType #withdraw').removeClass('active');
				$('#paymentType #payment').removeClass('active');
				$('#paymentType #recharge').addClass('active');	
				transactionType = 5 ;							
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//提现记录
			$("#withdraw").on("click",function(){				
				$('#paymentType #recharge').removeClass('active');
				$('#paymentType #all').removeClass('active');
				$('#paymentType #payment').removeClass('active');
				$('#paymentType #withdraw').addClass('active');
				transactionType = 4 ;
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//支付记录
			$("#payment").on("click",function(){
				$('#paymentType #recharge').removeClass('active');
				$('#paymentType #all').removeClass('active');
				$('#paymentType #withdraw').removeClass('active');
				$('#paymentType #payment').addClass('active');
				transactionType = 6 ;				
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});			
			
			$("#searchBtn").on("click",function(){	
				dateArr[0] = $("#reqTimeStart").val();
				dateArr[1] = $("#reqTimeEnd").val();
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//今天
			$("#day").on("click",function(){					
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");					
				dateArr = date.dateChange( "thisday" );				
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//近一周
			$("#week").on("click",function(){
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");
				dateArr = date.dateChange( "lastsevendays" );
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//近一月
			$("#month").on("click",function(){
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");
				dateArr = date.dateChange( "recentmonth" );
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//近三月
			$("#three").on("click",function(){
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");
				dateArr = date.dateChange( "threemonth" );
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
			//一年
			$("#year").on("click",function(){
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");
				dateArr = date.dateChange( "recentyear" );
				_this.load(transactionType,dateArr[0],dateArr[1]);
			});
		},
		
		sumCashAmt: function (dataResult,type){
			var cashSum =  new Array();	
			var sumCashIncomeAmt = 0.00 ;
			var sumCashExpenditureAmt = 0.00 ;
			for(index in dataResult){
				var data = dataResult[index];
				if(data.dealType.index == type){
					var cashIncomeAmt = data.amount;
					if(!!cashIncomeAmt){
						sumCashIncomeAmt += parseFloat(cashIncomeAmt); 
					}					
					
				}else{
					var cashExpenditureAmt = data.amount;
					if(!!cashExpenditureAmt){
						sumCashExpenditureAmt += parseFloat(cashExpenditureAmt);
					}
				} 
			}
			cashSum[0] = sumCashIncomeAmt;
			cashSum[1] = sumCashExpenditureAmt;
			return cashSum ;
		},
		
		
		load : function (type,reqTimeStart,reqTimeEnd){		
			var dealVo = {
				dealType:type,
				reqTimeStart:reqTimeStart,
			    reqTimeEnd:reqTimeEnd,
			    userId:$("#userId").val(),
    		};
			if(!!_this.walletPage){
				_this.walletPage.page("remote",0,dealVo);
			}else{
				_this.walletPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.initListURL,
			        params: dealVo, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		if(null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1
												|| null == data.result.itemList[0]){
			        			$("#page_grid").html('<tr><td align="center" colspan="15">暂无数据</td></tr>');
			        		}else{			        			
			        			$("#page_grid").html($("#pageTmp").render(data.result.itemList));
			        			var sumCashAmt = myWalletCtrl.myWallet.sumCashAmt(data.result.itemList,5);
			        			var cashIncomeAmt = moneyCtrl.money.fmoney(sumCashAmt[0],2);
			        			var cashExpenditureAmt = moneyCtrl.money.fmoney(sumCashAmt[1],2);
			        			$("#cashIncomeAmt").html("收入合计："+cashIncomeAmt+" 元");
			        			$("#cashExpenditureAmt").html("支出合计："+cashExpenditureAmt+" 元");
			        		}
			        		
			        	}else{
			        		  window.webDialogUtil.alert("提示",data.msg);
			        	}
			        },
			        error : function(){
			        	window.webDialogUtil.alert("提示",data.msg);
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		
		
		
},

$(function(){	
	
	myWalletCtrl.myWallet.event();	
	var date = new Date(); 
	var dateArr = date.dateChange( "thisday" ); //默认查询当天
	myWalletCtrl.myWallet.load(0,dateArr[0],dateArr[1]);
	
});